package com.xidian.zhiancloud.controller.ledgerManagerment.targetAndSystem;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.constant.LedgerManagementConstant;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.dto.xidianzyf.targetAndSystem.SecurityInputLedgerDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exceldto.ledgermanagerment.SecurityInputLedgerExcelDTO;
import com.xidian.zhiancloud.model.SecurityInputLedgerDetail;
import com.xidian.zhiancloud.qo.ledgerManagerment.targetAndSystem.SecurityInputLedgerQO;
import com.xidian.zhiancloud.service.ledgerManager.targerAndSystem.SecurityInputLedgerService;
import com.xidian.zhiancloud.util.zyf.StringUtils;
import com.xidian.zhiancloud.vo.ledgerManagerment.targetAndSystem.SecurityInputLedgerDTOVO;
import com.xidian.zhiancloud.vo.ledgerManagerment.targetAndSystem.SecurityInputLedgerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 台账管理 目标职责与制度 安全投入台账
 *
 * @Author: Summerless
 * @Date: 2020/11/9 11:31
 */
@RestController
@RequestMapping("/SecurityInputLedger")
public class SecurityInputLedgerController extends BaseController {
    @Autowired
    private SecurityInputLedgerService securityInputLedgerService;
    @Autowired
    private OrganizationMapper organizationMapper;

    /**
     * 增加安全投入台账对象
     *
     * @param securityInputLedgerDTO 安全投入台账对象
     * @return 是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(SecurityInputLedgerDTO securityInputLedgerDTO) {
        securityInputLedgerDTO.getSecurityInputLedger().setCreatorId(getSessionUserid());
        securityInputLedgerDTO.getSecurityInputLedger().setCompanyId(getSessionUserCompanyId());
        securityInputLedgerDTO.getSecurityInputLedger().setCreateTime(new Date(System.currentTimeMillis()));
        Boolean res = securityInputLedgerService.add(securityInputLedgerDTO);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id查找安全投入台账对象
     *
     * @param id 安全投入台账对象的id
     * @return 安全投入台账对象
     */
    @GetMapping("/getById")
    public ResultDTO<SecurityInputLedgerDTOVO> getById(@RequestParam("id") Long id) {
        return ResultDTO.successOf(securityInputLedgerService.getById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id列表删除安全投入台账对象
     *
     * @param ids 安全投入台账对象的id列表
     * @return 是否删除成功
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        Boolean res = securityInputLedgerService.deleteByIds(ids);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 修改安全投入台账对象
     *
     * @param securityInputLedgerDTO 安全投入台账对象
     * @return 是否修改成功
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(SecurityInputLedgerDTO securityInputLedgerDTO) {
        securityInputLedgerDTO.getSecurityInputLedger().setCompanyId(null);
        securityInputLedgerDTO.getSecurityInputLedger().setCreatorId(null);
        securityInputLedgerDTO.getSecurityInputLedger().setCreateTime(null);
        Boolean res = securityInputLedgerService.patchById(securityInputLedgerDTO);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 列表查询
     *
     * @param securityInputLedgerQO 查询参数
     * @return 安全投入台账对象列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<SecurityInputLedgerVO>> getListByQO(SecurityInputLedgerQO securityInputLedgerQO) {
        if (securityInputLedgerQO.getCurrent() == null || securityInputLedgerQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        securityInputLedgerQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(securityInputLedgerService.selectByQO(securityInputLedgerQO), GeneralResponseEnums.SUCCESS);
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response, Long[] ids) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("安全投入台账导出", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<SecurityInputLedgerDTOVO> list = new LinkedList<>();
        for (Long id : ids) {
            SecurityInputLedgerDTOVO dtovo = securityInputLedgerService.getById(id);
            if(dtovo !=null){
                list.add(dtovo);
            }
        }
        List<SecurityInputLedgerExcelDTO> excelDTOS = new LinkedList<>();
        for (SecurityInputLedgerDTOVO dtovo : list) {
            List<SecurityInputLedgerDetail> details = dtovo.getDetails();

            for (SecurityInputLedgerDetail detail : details) {
                SecurityInputLedgerExcelDTO excelDTO = new SecurityInputLedgerExcelDTO();
                excelDTO.setYear(dtovo.getSecurityInputLedgerVO().getYear());
                excelDTO.setProjectTypeName(LedgerManagementConstant.ProjectType.get(dtovo.getSecurityInputLedgerVO().getProjectType()));
                excelDTO.setMoney(detail.getM1());
                excelDTO.setDetail(detail.getDetail());
                excelDTO.setPrincipalName(dtovo.getSecurityInputLedgerVO().getPrincipalName());
                excelDTO.setUseOrganizationNames(StringUtils.concatStrings(dtovo.getSecurityInputLedgerVO().getUseOrganizationNames()));
                excelDTO.setTime(dtovo.getSecurityInputLedgerVO().getCreateTime());
                excelDTO.setApproveName(dtovo.getSecurityInputLedgerVO().getApproveName());
                excelDTOS.add(excelDTO);

                SecurityInputLedgerExcelDTO excelDTO1 = new SecurityInputLedgerExcelDTO();
                excelDTO1.setYear(dtovo.getSecurityInputLedgerVO().getYear());
                excelDTO1.setProjectTypeName(LedgerManagementConstant.ProjectType.get(dtovo.getSecurityInputLedgerVO().getProjectType()));
                excelDTO1.setMoney(detail.getM2());
                excelDTO1.setDetail(detail.getDetail());
                excelDTO1.setPrincipalName(dtovo.getSecurityInputLedgerVO().getPrincipalName());
                excelDTO1.setUseOrganizationNames(StringUtils.concatStrings(dtovo.getSecurityInputLedgerVO().getUseOrganizationNames()));
                excelDTO1.setTime(dtovo.getSecurityInputLedgerVO().getCreateTime());
                excelDTO1.setApproveName(dtovo.getSecurityInputLedgerVO().getApproveName());
                excelDTOS.add(excelDTO1);

                SecurityInputLedgerExcelDTO excelDTO2 = new SecurityInputLedgerExcelDTO();
                excelDTO2.setYear(dtovo.getSecurityInputLedgerVO().getYear());
                excelDTO2.setProjectTypeName(LedgerManagementConstant.ProjectType.get(dtovo.getSecurityInputLedgerVO().getProjectType()));
                excelDTO2.setMoney(detail.getM3());
                excelDTO2.setDetail(detail.getDetail());
                excelDTO2.setPrincipalName(dtovo.getSecurityInputLedgerVO().getPrincipalName());
                excelDTO2.setUseOrganizationNames(StringUtils.concatStrings(dtovo.getSecurityInputLedgerVO().getUseOrganizationNames()));
                excelDTO2.setTime(dtovo.getSecurityInputLedgerVO().getCreateTime());
                excelDTO2.setApproveName(dtovo.getSecurityInputLedgerVO().getApproveName());
                excelDTOS.add(excelDTO2);

                SecurityInputLedgerExcelDTO excelDTO3 = new SecurityInputLedgerExcelDTO();
                excelDTO3.setYear(dtovo.getSecurityInputLedgerVO().getYear());
                excelDTO3.setProjectTypeName(LedgerManagementConstant.ProjectType.get(dtovo.getSecurityInputLedgerVO().getProjectType()));
                excelDTO3.setMoney(detail.getM4());
                excelDTO3.setDetail(detail.getDetail());
                excelDTO3.setPrincipalName(dtovo.getSecurityInputLedgerVO().getPrincipalName());
                excelDTO3.setUseOrganizationNames(StringUtils.concatStrings(dtovo.getSecurityInputLedgerVO().getUseOrganizationNames()));
                excelDTO3.setTime(dtovo.getSecurityInputLedgerVO().getCreateTime());
                excelDTO3.setApproveName(dtovo.getSecurityInputLedgerVO().getApproveName());
                excelDTOS.add(excelDTO3);

                SecurityInputLedgerExcelDTO excelDTO4 = new SecurityInputLedgerExcelDTO();
                excelDTO4.setYear(dtovo.getSecurityInputLedgerVO().getYear());
                excelDTO4.setProjectTypeName(LedgerManagementConstant.ProjectType.get(dtovo.getSecurityInputLedgerVO().getProjectType()));
                excelDTO4.setMoney(detail.getM5());
                excelDTO4.setDetail(detail.getDetail());
                excelDTO4.setPrincipalName(dtovo.getSecurityInputLedgerVO().getPrincipalName());
                excelDTO4.setUseOrganizationNames(StringUtils.concatStrings(dtovo.getSecurityInputLedgerVO().getUseOrganizationNames()));
                excelDTO4.setTime(dtovo.getSecurityInputLedgerVO().getCreateTime());
                excelDTO4.setApproveName(dtovo.getSecurityInputLedgerVO().getApproveName());
                excelDTOS.add(excelDTO4);

                SecurityInputLedgerExcelDTO excelDTO5 = new SecurityInputLedgerExcelDTO();
                excelDTO5.setYear(dtovo.getSecurityInputLedgerVO().getYear());
                excelDTO5.setProjectTypeName(LedgerManagementConstant.ProjectType.get(dtovo.getSecurityInputLedgerVO().getProjectType()));
                excelDTO5.setMoney(detail.getM6());
                excelDTO5.setDetail(detail.getDetail());
                excelDTO5.setPrincipalName(dtovo.getSecurityInputLedgerVO().getPrincipalName());
                excelDTO5.setUseOrganizationNames(StringUtils.concatStrings(dtovo.getSecurityInputLedgerVO().getUseOrganizationNames()));
                excelDTO5.setTime(dtovo.getSecurityInputLedgerVO().getCreateTime());
                excelDTO5.setApproveName(dtovo.getSecurityInputLedgerVO().getApproveName());
                excelDTOS.add(excelDTO5);

                SecurityInputLedgerExcelDTO excelDTO6 = new SecurityInputLedgerExcelDTO();
                excelDTO6.setYear(dtovo.getSecurityInputLedgerVO().getYear());
                excelDTO6.setProjectTypeName(LedgerManagementConstant.ProjectType.get(dtovo.getSecurityInputLedgerVO().getProjectType()));
                excelDTO6.setMoney(detail.getM7());
                excelDTO6.setDetail(detail.getDetail());
                excelDTO6.setPrincipalName(dtovo.getSecurityInputLedgerVO().getPrincipalName());
                excelDTO6.setUseOrganizationNames(StringUtils.concatStrings(dtovo.getSecurityInputLedgerVO().getUseOrganizationNames()));
                excelDTO6.setTime(dtovo.getSecurityInputLedgerVO().getCreateTime());
                excelDTO6.setApproveName(dtovo.getSecurityInputLedgerVO().getApproveName());
                excelDTOS.add(excelDTO6);

                SecurityInputLedgerExcelDTO excelDTO7 = new SecurityInputLedgerExcelDTO();
                excelDTO7.setYear(dtovo.getSecurityInputLedgerVO().getYear());
                excelDTO7.setProjectTypeName(LedgerManagementConstant.ProjectType.get(dtovo.getSecurityInputLedgerVO().getProjectType()));
                excelDTO7.setMoney(detail.getM8());
                excelDTO7.setDetail(detail.getDetail());
                excelDTO7.setPrincipalName(dtovo.getSecurityInputLedgerVO().getPrincipalName());
                excelDTO7.setUseOrganizationNames(StringUtils.concatStrings(dtovo.getSecurityInputLedgerVO().getUseOrganizationNames()));
                excelDTO7.setTime(dtovo.getSecurityInputLedgerVO().getCreateTime());
                excelDTO7.setApproveName(dtovo.getSecurityInputLedgerVO().getApproveName());
                excelDTOS.add(excelDTO7);

                SecurityInputLedgerExcelDTO excelDTO8 = new SecurityInputLedgerExcelDTO();
                excelDTO8.setYear(dtovo.getSecurityInputLedgerVO().getYear());
                excelDTO8.setProjectTypeName(LedgerManagementConstant.ProjectType.get(dtovo.getSecurityInputLedgerVO().getProjectType()));
                excelDTO8.setMoney(detail.getM9());
                excelDTO8.setDetail(detail.getDetail());
                excelDTO8.setPrincipalName(dtovo.getSecurityInputLedgerVO().getPrincipalName());
                excelDTO8.setUseOrganizationNames(StringUtils.concatStrings(dtovo.getSecurityInputLedgerVO().getUseOrganizationNames()));
                excelDTO8.setTime(dtovo.getSecurityInputLedgerVO().getCreateTime());
                excelDTO8.setApproveName(dtovo.getSecurityInputLedgerVO().getApproveName());
                excelDTOS.add(excelDTO8);

                SecurityInputLedgerExcelDTO excelDTO9 = new SecurityInputLedgerExcelDTO();
                excelDTO9.setYear(dtovo.getSecurityInputLedgerVO().getYear());
                excelDTO9.setProjectTypeName(LedgerManagementConstant.ProjectType.get(dtovo.getSecurityInputLedgerVO().getProjectType()));
                excelDTO9.setMoney(detail.getM10());
                excelDTO9.setDetail(detail.getDetail());
                excelDTO9.setPrincipalName(dtovo.getSecurityInputLedgerVO().getPrincipalName());
                excelDTO9.setUseOrganizationNames(StringUtils.concatStrings(dtovo.getSecurityInputLedgerVO().getUseOrganizationNames()));
                excelDTO9.setTime(dtovo.getSecurityInputLedgerVO().getCreateTime());
                excelDTO9.setApproveName(dtovo.getSecurityInputLedgerVO().getApproveName());
                excelDTOS.add(excelDTO9);

                SecurityInputLedgerExcelDTO excelDTO10 = new SecurityInputLedgerExcelDTO();
                excelDTO10.setYear(dtovo.getSecurityInputLedgerVO().getYear());
                excelDTO10.setProjectTypeName(LedgerManagementConstant.ProjectType.get(dtovo.getSecurityInputLedgerVO().getProjectType()));
                excelDTO10.setMoney(detail.getM11());
                excelDTO10.setDetail(detail.getDetail());
                excelDTO10.setPrincipalName(dtovo.getSecurityInputLedgerVO().getPrincipalName());
                excelDTO10.setUseOrganizationNames(StringUtils.concatStrings(dtovo.getSecurityInputLedgerVO().getUseOrganizationNames()));
                excelDTO10.setTime(dtovo.getSecurityInputLedgerVO().getCreateTime());
                excelDTO10.setApproveName(dtovo.getSecurityInputLedgerVO().getApproveName());
                excelDTOS.add(excelDTO10);

                SecurityInputLedgerExcelDTO excelDTO11 = new SecurityInputLedgerExcelDTO();
                excelDTO11.setYear(dtovo.getSecurityInputLedgerVO().getYear());
                excelDTO11.setProjectTypeName(LedgerManagementConstant.ProjectType.get(dtovo.getSecurityInputLedgerVO().getProjectType()));
                excelDTO11.setMoney(detail.getM12());
                excelDTO11.setDetail(detail.getDetail());
                excelDTO11.setPrincipalName(dtovo.getSecurityInputLedgerVO().getPrincipalName());
                excelDTO11.setUseOrganizationNames(StringUtils.concatStrings(dtovo.getSecurityInputLedgerVO().getUseOrganizationNames()));
                excelDTO11.setTime(dtovo.getSecurityInputLedgerVO().getCreateTime());
                excelDTO11.setApproveName(dtovo.getSecurityInputLedgerVO().getApproveName());
                excelDTOS.add(excelDTO11);
            }
        }
        System.out.println(excelDTOS.size());
        EasyExcel.write(response.getOutputStream(), SecurityInputLedgerExcelDTO.class).sheet("模板").doWrite(excelDTOS);
    }
}
